#coding:utf-8
#1.获取同步文件列表
#2，对比差异 ，生成差异文件列表
#3，同步
import os,sys,getopt
import MySQLdb
import multiprocessing
import re
from subprocess import Popen,PIPE
import time

def usage():
    print "-d "

work_dir="/home/jobs/rsynclog"
try:
    opts, args = getopt.getopt(sys.argv[1:], "hd:l:")
except getopt.GetoptError:
    print("opt error")
for name,value in opts:

    if name=="-d":
        mday=value
    if name=="-l":
        logname=value
    if name=="-h":
        usage()
        exit()
try:
    conn=MySQLdb.connect(host='192.168.1.28',user='root',passwd='work',port=3306)
    cur=conn.cursor()
    conn.select_db('work')
except MySQLdb.Error:
    print ("Mysql Error ")
sql="select concat(yq_ip,\"::\",yq_name)  logmod from logcollect where logname=\""+str(logname)+"\";"
cur.execute(sql)
logmod=cur.fetchone()[0]
sql="select tr_dir   from logcollect where logname=\""+str(logname)+"\";"
cur.execute(sql)
tr_dir=cur.fetchone()[0]
print "logmod is %s tr_dir is %s" %(logmod ,tr_dir)
print mday

#
file_dir=work_dir+"/fill_log/"+str(logmod)
rsync_file=str(file_dir)+"/"+str(mday)
rsync_r_cmd="rsync -r "+str(logmod)+" |grep "+str(mday)+" | awk '{print $5}' > "+str(rsync_file)
rsync_e_file=rsync_file+"_exits"                #已同步的文件列表
rsync_l_file=rsync_file+"_lost"                 #为同步的文件列表
exist_count=0
no_exist_count=0


if  not os.path.exists(file_dir):
    os.mkdir(file_dir)


print rsync_r_cmd
p=Popen(rsync_r_cmd,shell=True,stdout=PIPE,stdin=PIPE)
print str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))+ "获取文件列表,同步进程pid:"+str(p.pid)
p.wait()
print str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))+ "获取完成"



f=open(rsync_file,"r")
r_filelist=[]
for i in f.readlines():
    r_filelist.append(i.strip("\n"))
f.close()

all_file=len(r_filelist)
print all_file
fe=open(rsync_e_file,"w")
fl=open(rsync_l_file,"w")
for i in r_filelist:
    if re.match('.*\.gz', i):
        i_aname = i.replace(".gz", '')
    else:
        i_aname = i + ".gz"

    if os.path.exists(tr_dir+"/"+i) or os.path.exists(tr_dir+"/"+i_aname):
        fe.write(i+"\n")
        exist_count=exist_count+1
    else:
        fl.write(i+"\n")
        no_exist_count=no_exist_count+1
fe.close()
fl.close()
raw=raw_input("开始同步差异文件停止:n \n" )
if raw=="n":
    exit()

rsync_r_cmd="rsync --files-from="+str(rsync_l_file)+" -avz "+str(logmod)+" "+tr_dir
print rsync_r_cmd
p=Popen(rsync_r_cmd,shell=True,stdin=PIPE,stdout=PIPE)

print str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))+ "开始同步"

p.wait()
print str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))+ "开始完成"





